package com.cysion.otbox.ext

import android.app.Activity
import android.os.Build
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.widget.AdapterView
import androidx.annotation.ColorInt
import com.cysion.otbox.Box
import com.jaeger.library.StatusBarUtil
import com.orhanobut.logger.Logger
import java.lang.Exception

/**
 * 简略打印
 */
val TAG = "ktDevBox"

fun logd(msg: String) {
    if (Box.debug) {
        Logger.d(msg)
    }
}

fun logObj(msg: Any) {
    if (Box.debug) {
        Logger.d(msg)
    }
}

fun logi(msg: String) {
    if (Box.debug) {
        Logger.i(msg)
    }
}

fun logw(msg: String) {
    if (Box.debug) {
        Logger.w(msg)
    }
}


//深度回收view
fun View.gc() {
    val view = this
    if (view.background != null) {
        view.background.callback = null
    }
    if (view is ViewGroup && view !is AdapterView<*>) {
        if (view is WebView) {
            view.removeAllViews()
            view.destroy()
            return
        }
        for (i in 0 until view.childCount) {
            view.getChildAt(i).gc()
        }
        view.removeAllViews()
    }
}

fun tryCatch(task: () -> Unit) {
    try {
        task()
    } catch (e: Exception) {
        logObj(e)
    }
}

fun <T> tryCatchWithReSult(task: () -> T):T? {
    try {
        return task()
    } catch (e: Exception) {
        logObj(e)
    }
    return null
}

